package com.fw.service.impl;

import java.util.List;

import com.fw.dao.PermissionDao;
import com.fw.dao.impl.PermissionDaoImpl;
import com.fw.model.Permission;
import com.fw.service.PermissionService;

public class PermissionServiceImpl implements PermissionService {
	PermissionDao permissonDao = null;
	@Override
	public Integer query_totalPermissonService(String sql) {
		permissonDao = new PermissionDaoImpl();
		return permissonDao.query_totalPermisssonDao(sql);
	}

	@Override
	public List<Permission> query_allPermissonByPageService(String sql,int firstPage, int maxPage) {
		permissonDao = new PermissionDaoImpl();
		List<Permission> ps = permissonDao.query_allPermissonByPageDao(sql, firstPage, maxPage);
		if(ps.size() >0){
			for(Permission p : ps){
				p.put("permission",p.getParentPermission());
			}
		}
		return ps;
	}
	@Override
	public boolean add_permissionService(Permission permission) {
		permissonDao = new PermissionDaoImpl();
		Permission p = permissonDao.query_permissionByName(permission);
		if(p != null) return false;
		return permissonDao.add_permissionDao(permission);
	}
	@Override
	public List<Permission> query_permissionForTypeDao(Permission permission) {
		permissonDao = new PermissionDaoImpl();
		return permissonDao.query_permissionForTypeDao(permission);
	}

	@Override
	public Permission query_permissionByIdService(Permission permission) {
		permissonDao = new PermissionDaoImpl();
		return permissonDao.query_permissionByIdDao(permission);
	}

	@Override
	public boolean update_permissionService(Permission permission) {
		permissonDao = new PermissionDaoImpl();
		Permission p = permissonDao.query_permissionByName(permission);
		if(p != null && p.getInt("p_id") != permission.getInt("p_id")) return false;
		return permissonDao.update_permissionByIdDao(permission);
	}

	@Override
	public boolean delete_permissionByIdService(Permission permission) {
		permissonDao = new PermissionDaoImpl();
		return permissonDao.delete_permissionByIdDao(permission);
	}

}
